package com.controller;

import com.domain.Message;
import com.domain.Order;
import com.domain.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.mapper.Ordermapper;
import com.mysql.cj.xdevapi.JsonArray;
import com.mysql.cj.xdevapi.JsonValue;
import com.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.UserloginService;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;


@Controller
@RequestMapping("/gst")
public class Usercontroller {


    @Autowired
    private UserloginService userservice;


    @Autowired
    private OrderService orderservice;
    @RequestMapping(value = "/choose")
    public ModelAndView modelAndView(HttpServletRequest request,  ServletResponse response)  {
        String name=(String) request.getParameter("name");
        String pwd=(String) request.getParameter("pwd");
        User user =new User() ;
        user.setName(name);
        user.setPwd(pwd);
        List<User> search1=userservice.findalluser();
        List<Order> search2=orderservice.findallorder();
        /*  response.setContentType("text/html;charset=utf-8");*/
        request.getSession().setAttribute("USER_SESSION","exist");
        ModelAndView modelAndView=new ModelAndView();

        try {
            if (name.equals("") || pwd.equals("")) {
                JOptionPane.showMessageDialog(null, "输入数值不能为空");
                modelAndView.setViewName("/index");
              return modelAndView;

            }

            for (User one : search1) {
                if (name.equals(one.name) && pwd.equals(one.pwd)) {
                    JOptionPane.showMessageDialog(null, "登录成功");
                    modelAndView.setViewName("/choose");
                    return modelAndView;
                }
            }


       /*     JOptionPane.showMessageDialog(null, "不存在该账号或密码");
        modelAndView.setViewName("index");*/

          /*  json.add(new Message("登录失败！不存在该账号或密码"));*/
            JOptionPane.showMessageDialog(null, "登录失败！不存在该账号或密码");
            modelAndView.setViewName("/index");
            return modelAndView;

        }catch (Exception e) {
            e.printStackTrace();
        /*    json.add(new Message("被拦截！不可访问该信息！你还没有登录"));
            return json;*/

            JOptionPane.showMessageDialog(null, "被拦截！不可访问该信息！你还没有登录");
            modelAndView.setViewName("/index");
            return modelAndView;



        }

    }

    @RequestMapping(value = "/findalluser",produces = "application/json;charset=utf-8")
    @ResponseBody
    public JsonArray json1(HttpServletRequest request,  ServletResponse response)  {
        List<User> search1=userservice.findalluser();
        List<Order> search2=orderservice.findallorder();
       String message=(String) request.getAttribute("USER_SESSION");
        JsonArray json=new JsonArray();
        if(message==null)
        {

            json.add(new Message("被拦截！不可访问该信息！你还没有登录"));
            return json;

        }
        try {
            json.add(new Message("登录成功"));
            for (Order order1 : search2) {
                json.add(order1);
            }

                    return json;
        }catch (Exception e) {
            e.printStackTrace();
            json.add(new Message("被拦截！不可访问该信息！你还没有登录"));
            return json;
        }


    }


    @RequestMapping("index")
    public ModelAndView modelAndView(HttpServletRequest request,HttpServletResponse response)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
      return  modelAndView;

    }
}
